package com.qlk.odcinterface.rest;

import com.qlk.common.utils.serialize.JsonHelper;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.odcinterface.domain.PmsFullCut;
import com.qlk.odcinterface.domain.PmsFullCutChannel;
import com.qlk.odcinterface.sdk.api.request.OdcRequest;
import com.qlk.odcinterface.sdk.api.request.PmsFullCutRequest;
import com.qlk.odcinterface.sdk.api.response.PmsFullCutResponse;
import com.qlk.odcinterface.service.PmsFullCutService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.resteasy.annotations.Form;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 满减表REST服务：提供有关满减表的接口
 * 
 * @author zhaoyanan
 * @version 2016-4-14 17:10:21
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
public class PmsFullCutResource {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PmsFullCutService pmsFullCutService;

    /**
     * 同步满减信息
     * @param request
     * @return 满减表返回对象
     */
    @POST
    @Path("/pmsFullCut/syncPmsFullCut")
    public Wrapper<?> syncPmsFullCutInfo(@Form OdcRequest request) {

        try {
            if (null == request || StringUtils.isEmpty(request.getContent())) {
                this.logger.error("getPmsFullCut 传入报文为空！");
                return WrapMapper.illegalArgument();
            }
            String content = request.getContent();
            PmsFullCutRequest fullCutRequest = JsonHelper.toBean(content,PmsFullCutRequest.class);
            Long promotionId = fullCutRequest.getId();
            String channel = fullCutRequest.getChannel();
            //将满减信息转换成对应的json形式
            String fullFill = fullCutRequest.getFullfill(); //满多少
            String reducePrice = fullCutRequest.getReducePrice();//减多少
            fullCutRequest.setReduceInfo(covertFullCutRules(fullFill,reducePrice));
            //转换渠道信息  String --> list
            List<PmsFullCutChannel> channelList = convertChannelToList(channel,promotionId);
            fullCutRequest.setChannelList(channelList);
            //同步信息
            pmsFullCutService.syncFullCutInfo(fullCutRequest,request.getIsStatusUpdate());
            return WrapMapper.ok();
        } catch (Exception e) {
            this.logger.error("syncPmsFullCutInfo同步数据异常", e);
            return WrapMapper.error();
        }
    }

    /**
     * string --》 list
     * @param channel
     * @param promotionId
     * @return
     */
    private List<PmsFullCutChannel> convertChannelToList(String channel,Long promotionId){
        if (StringUtils.isEmpty(channel)){
            return null;
        }
        String[] channelArray = channel.split(",");
        List<PmsFullCutChannel> rs = new ArrayList<PmsFullCutChannel>();
        PmsFullCutChannel pmsFullCutChannel = null;
        for (int i=0; i<channelArray.length; i++){
            pmsFullCutChannel = new PmsFullCutChannel();
            pmsFullCutChannel.setPromotionId(promotionId);
            pmsFullCutChannel.setChannelId(Integer.valueOf(channelArray[i]));
            rs.add(pmsFullCutChannel);
        }
        return rs;
    }

    /**
     *
     * @param fullFill 满多少
     * @param reduce 减多少
     * @return json
     */
    private String covertFullCutRules(String fullFill, String reduce){
        if(StringUtils.isEmpty(fullFill)||StringUtils.isEmpty(reduce)){
            return "";
        }
        try{
            String[] fullFillArray = fullFill.split(",");
            String[] reduceArray = reduce.split(",");
            List<Map<String,String>> rs = new ArrayList<Map<String, String>>();
            Map<String,String> item = new HashMap<String, String>();
            for (int i=0;i<fullFillArray.length;i++){
                item.put("fullfill",fullFillArray[i]);
                item.put("reduceprice",reduceArray[i]);
                rs.add(item);
            }
            return JsonHelper.toJson(rs);
        }catch (Exception e){
            logger.error("covertFullCutRules error!" + e.getMessage());
            return "";
        }
    }


    // 数据转换
    private PmsFullCutResponse convert(PmsFullCut pmsFullCut) {
        if (null == pmsFullCut) {
            return null;
        }

        PmsFullCutResponse pmsFullCutResponse = new PmsFullCutResponse();
        BeanUtils.copyProperties(pmsFullCut, pmsFullCutResponse);
        return pmsFullCutResponse;
    }

    // 数据转换
    private List<PmsFullCutResponse> convertList(List<PmsFullCut> pmsFullCuts) {
        if (CollectionUtils.isEmpty(pmsFullCuts)) {
            return null;
        }

        List<PmsFullCutResponse> list = new ArrayList<PmsFullCutResponse>(pmsFullCuts.size());
        for (PmsFullCut pmsFullCut : pmsFullCuts) {
            list.add(convert(pmsFullCut));
        }
        return list;
    } 

}
